﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BusinessLogic.ClassFactory;
using ValidateCommon;
using IssueCommon;
using Common;
namespace RichECMS.ServerPages.Js
{
    public partial class Update : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LitParentPath.Text = HttpContext.Current.Server.MapPath("~/") + @"scripts\common\";
                InitDataBind();
                if (Request["Id"] != null)
                {
                    if (ParamAuth.IsNumber(Request["Id"].ToString().Trim()))
                    {
                        InitDataBind( int.Parse(Request["id"].ToString()));
                    }
                }
            }
        }

        //信息显示
        private void InitDataBind(int intIcriptID)
        {
            using (ScriptsFactory objFactory = new ScriptsFactory())
            {
                try
                {
                    BusinessLogic.BusinessRules.Scripts objScript = objFactory.Creator(intIcriptID);
                    if (objScript != null)
                    {
                        txtTitle.Value = objScript.DataItem.chvScriptsName;
                       txtTemplatePath.Value = objScript.DataItem.chvScriptsUrl;
                        txtMemo.Value = objScript.DataItem.chvJsMemo;
                        txtContent.Value = objScript.DataItem.textContent;
                        DropDownListType.ClearSelection();
                        DropDownListType.Items.FindByValue(objScript.DataItem.intType.ToString()).Selected = true;
                        rbtnListEnableUse.SelectedValue = objScript.DataItem.intState.ToString();
                    }
                    else
                    {
                       Alert("文章不存在");
                    }
                }
                catch (Exception ex)
                {
                    Alert(ex.ToString().Replace("\r\n", " "));
                }
            }
        }


        /// <summary>
        /// 执行上传
        /// </summary>
        private string UploadExec()
        {
            if (FileUploadTemplate.FileName.Length != 0)
            {
                string strfilepath = HttpContext.Current.Server.MapPath("~/") + "/scripts/common/" + FileUploadTemplate.FileName;
                FileUploadTemplate.SaveAs(strfilepath);
                return FileUploadTemplate.FileName;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 页面输出
        /// </summary>
        private void InitDataBind()
        {
            //绑定类型
            ListItem listitem = new ListItem("--请选择JS模版类型--", "-1");
            DropDownListType.Items.Add(listitem);
            foreach (int i in Enum.GetValues(typeof(Utility.JSTemplateType)))
            {
                listitem = new ListItem(Utility.GetJSTemplateType(i), i.ToString());
                DropDownListType.Items.Add(listitem);
            }
        }

        /// <summary>
        /// 验证输入
        /// </summary>
        private bool ValidateValues()
        {
            bool bolIsValidate = true;
            if (this.txtTitle.Value.Trim().Length == 0)
            {
                Alert("请填写标题");
                bolIsValidate = false;
            }
            if (DropDownListType.SelectedValue.Trim() == "-1")
            {
                Alert("请选择JS模版类型");
                bolIsValidate = false;
            }
            return bolIsValidate;
        }

        //修改
        protected void SubmitBtnHander(object sender, ImageClickEventArgs e)
        {
            if (!ValidateValues())
            {
                return;
            }

            using (ScriptsFactory objFactory = new ScriptsFactory())
            {
                try
                {
                    BusinessLogic.BusinessRules.Scripts objScript = objFactory.Creator(int.Parse(Request["id"].ToString()));
                    objScript.DataItem.chvScriptsName = txtTitle.Value;
                    if (FileUploadTemplate.FileName.Trim().Length != 0)
                    {
                        objScript.DataItem.chvScriptsUrl = UploadExec();
                    }
                    else
                    {
                        objScript.DataItem.chvScriptsUrl = this.txtTemplatePath.Value;
                    }
                    objScript.DataItem.chvJsMemo = txtMemo.Value;
                    objScript.DataItem.textContent = txtContent.Value;
                    objScript.DataItem.intState =int.Parse(rbtnListEnableUse.SelectedValue);
                    objScript.DataItem.intType =int.Parse(DropDownListType.SelectedValue);
                    if (objScript.Update() > 0)
                    {
                        AlertAndRedirect("修改成功", "list.aspx", false);
                    }
                    else
                    {
                        Alert("修改失败");
                    }
                }
                catch (Exception ex)
                {
                    Alert(ex.ToString().Replace("\r\n", " "));
                }
            }
        }
    }
}